帶有 -C 選項的 pg_restore 不會創建數據庫 (pg_restore with -C option does not create the database)


問題描述

帶有 ‑C 選項的 pg_restore 不會創建數據庫 (pg_restore with ‑C option does not create the database)

我正在使用 pg_dump 和 pg_restore 來備份和恢復 postgres 數據庫。

這裡有一些與這個問題相關的文檔信息對於Pg_restore,‑C選項描述如下

‑C

‑ ‑create

在恢復之前創建數據庫。如果還指定了 ‑‑clean,> > 在連接到目標數據庫之前刪除並重新創建它。使用此選項時,以 ‑d 命名的數據庫僅用於發出初始 DROP DATABASE 和 CREATE DATABASE 命令。所有數據都恢復到存檔中出現的數據庫名稱中。

但是,即使我將此選項與 pg_restore 一起使用,我也會收到以下錯誤

pg_restore: [archiver (db)] connection to database "test" failed: FATAL: > database "test" does not exist

根據描述 ‑C 選項應該有創建了丟失的數據庫。但是在我的情況下它不是。

以下是我為備份和恢復所做的步驟:

  1. 使用pg_dump備份數據庫
pg_dump ‑N backup ‑d test ‑‑format custom ‑v ‑h xxhostxx ‑p 5432 ‑U xxuserxx ‑‑lock‑wait‑timeout 300000 ‑f test_pg_dump.dmp

注意:不使用‑C 選項,因為它僅對純文本格式有意義

  1. 已刪除 test 數據庫

  2. 使用pg_resore恢復數據庫

    pg_restore ‑C ‑d test ‑v ‑h xxhostxx ‑p 5432 ‑U xxuserxx test_pg_dump.dmp**
    

我不明白這裡有什麼問題!我做錯什麼了嗎?如果需要更多信息,請告訴我。


參考解法

方法 1:

Exactly like @Eelke said ‑ you've got in file wrote 'create database' so this database does not exist when you're running script... That's what for there is always 'postgres' database. Try this:

pg_restore ‑C ‑d postgres ‑v ‑h xxhostxx ‑p 5432 ‑U xxuserxx test_pg_dump.dmp**

And this should:

  1. connect to postgres database
  2. Create test database
  3. Disconnect from postgres and connect to test
  4. Upload data into database

Of course check who is owner of postgres database ‑ in most cases you have to run this as user 'postgres'.

方法 2:

It never worked for me so this command creates the database

createdb ‑h HOST ‑U USER ‑W DB_NAME

then execute the pg restore

pg_restore ‑d DB_NAME ‑v ‑h HOST ‑p PORT ‑U USER DUMP_FILE.dump**

End of story

方法 3:

The following quote doesn't mean what you might think it means. I also had to read it thrice before realizing what they were saying.

When this option is used, the database named with ‑d is used only to issue the initial DROP DATABASE and CREATE DATABASE commands. All data is restored into the database name that appears in the archive.

It means that pg_restore will initially connect to the database specified with ‑d. It will NOT create that database. It creates a database with the name from the archive you are restoring and restores the data into that database.

方法 4:

From man pg_restore EXAMPLES section

Assume we have dumped a database called mydb into a custom‑format dump file:

   $ pg_dump ‑Fc mydb > db.dump

To drop the database and recreate it from the dump:

   $ dropdb mydb
   $ pg_restore ‑C ‑d postgres db.dump

The database named in the ‑d switch can be any database existing in the cluster; pg_restore only uses it to issue the CREATE DATABASE command for mydb.
With ‑C, data is always restored into the database name that appears in the dump file.

To reload the dump into a new database called newdb:

   $ createdb ‑T template0 newdb
   $ pg_restore ‑d newdb db.dump

Notice we don't use ‑C, and instead connect directly to the database to be restored into. Also note that we clone the new database from template0 not
template1, to ensure it is initially empty.

Thus in your case:

$ createdb ‑h xxhostxx ‑p 5432 ‑U xxuserxx ‑T template0 test
$ pg_restore ‑h xxhostxx ‑p 5432 ‑U xxuserxx ‑d test db.dump

(by Swapnil17JendruskAndre Leon RangelEelkeborracciaBlu)

參考文件

  1. pg_restore with ‑C option does not create the database (CC BY‑SA 2.5/3.0/4.0)

#pg-dump #pg-restore #postgresql






相關問題

如何檢查我是否只刪除了所需的數據? (How do I check that I removed required data only?)

無法恢復 pg_dump 備份 (Unable to restore pg_dump backup)

使用帶有 Curl 的緩衝輸出將文件上傳到 ftp 服務器 (Upload a file into a ftp server using buffered output with Curl)

帶有 -C 選項的 pg_restore 不會創建數據庫 (pg_restore with -C option does not create the database)

pg_dump 數據庫轉儲是“當時”轉儲嗎? (Is a pg_dump DB dump 'at-that-time' dump?)

PSQL 數據庫傳輸和錯誤 (PSQL database transfer and errors)

Postgres pg_dump 顯示空文件 (Postgres pg_dump show empty file)

將 pg_restore 與多個轉儲一起使用時管理外鍵 (Managing foreign keys when using pg_restore with multiple dumps)

設計:在不斷創建和刪除表時運行 pg_dump (Design: running pg_dump when tables are continuously created and dropped)

轉儲文件中視圖預定義的目的是什麼 (What is the purpose of views' predefinitions in dump file)

如何附加 pg_dump 備份命令 PostgreSQL 的日誌輸出 (How to append log output of pg_dump backup command PostgreSQL)

PostgreSQL:單個表的 pg_dump (PostgreSQL: pg_dump for a single table)







留言討論